package com.laomuji.android.mvvm.utils.firebase

import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.analytics.logEvent
import com.google.firebase.ktx.Firebase
import com.google.firebase.messaging.FirebaseMessaging

/**
 * Firebase 例子
 *
 * 在 Firebase 的 DebugView 实时调试:
 * adb shell setprop debug.firebase.analytics.app com.laomuji.android.mvvm
 *
 * 查看Firebase 设备日志
 * adb shell setprop log.tag.FirebaseCrashlytics DEBUG
 * adb logcat -s FirebaseCrashlytics
 *
 * @author laomuji666
 * @since 2024/4/29
 */
object FirebaseTest {
    /**
     * Firebase 埋点
     */
    fun testFirebaseAnalytics(){
        val userInfo = System.currentTimeMillis()
        Firebase.analytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT){
            param(FirebaseAnalytics.Param.ITEM_ID, "${userInfo%1000000}")
            param(FirebaseAnalytics.Param.ITEM_NAME, "$userInfo")
            param(FirebaseAnalytics.Param.CONTENT_TYPE, "test")
        }
    }

    /**
     * Firebase 崩溃
     */
    fun testCrashlytics(){
        throw RuntimeException("Test Crash")
    }

    /**
     * 获取 FCM token
     */
    fun getPushTokenFCM(tokenCallback:(String)->Unit){
        FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
            if (!task.isSuccessful) {
                task.exception?.printStackTrace()
                return@OnCompleteListener
            }
            tokenCallback(task.result)
        })
    }
}